<?php
/**
 * vip用户到期提醒续费,到期的用户给改成一般用户.
 */
require_once(dirname(__FILE__)."/../config/include.php");
	
	

$GLOBALS['DEBUG_SQL_LOG'] = false ;


sql_connect() ;


function log_save_vip($logs)
{
	$time = date("Ymd H:i:s") ;
	$save_log_filename = dirname(__FILE__)."/log_core_vip_time.txt" ;
	file_put_contents($save_log_filename,"$time -- $logs .\r\n",FILE_APPEND);
}	
	
$tmend = 30 * 24 * 60 * 60 ; // 1月
	
$start = 0 ;
$limit = 50 ;

$message = "尊敬的VIP用户，您的VIP用户权限即将到期，如继续保有权限，请及时到JAJAHOME的个人中心续费。" ;

log_save_vip("开始搜索快到期用户:发送提醒短信") ;
	
while(true){
	$paylist = sql_fetch_rows("select * , unix_timestamp(from_unixtime(end_time,'%Y-%m-%d')) as ENDDAY , unix_timestamp(curdate()) as NOWDAY from `user_pay_vip` where unix_timestamp(from_unixtime(end_time,'%Y-%m-%d')) - $tmend <= unix_timestamp(curdate())  limit $start , $limit") ;
	if (is_array($paylist)) foreach($paylist as &$u){
		$tm = $u['ENDDAY'] - $u['NOWDAY'] ;
		$uid = $u['user_id'] ;
		if ($tm >= $tmend)
		{
			$mobile = sql_fetch_one_cell("select `tel` from `user_info` where `id` = $uid") ;
			$sd = send_mobile_SMS($mobile , $message) ;
			log_save_vip("给到期用户:".$uid." 发送短信提醒,到期时间还剩:".$tm) ;
		}else{
			log_save_vip("用户:".$uid." 到期时间还剩:".$tm) ;
		}
	}else{
		break ;
	}
	if (count($paylist) != $limit){
		break ;
	}
	$start = $start + $limit ;
}

$start = 0 ;
$limit = 50 ;

log_save_vip("开始搜索到期用户:修改状态") ;

while(true){
	
	$paylist = sql_fetch_rows("select * from `user_pay_vip` where unix_timestamp(from_unixtime(end_time,'%Y-%m-%d')) <= unix_timestamp(curdate()) limit $start , $limit") ;
	if (is_array($paylist)) foreach($paylist as &$u){
		
		$uid = $u['user_id'] ;
		sql_query("update `user_info` set `user_type_class` = '1' , `user_type` = 1  where `id` = '$uid'") ;
		
		log_save_vip("用户:".$uid." 付费VIP到期,改为普通用户") ;		
	}else{
		break ;
	}
	if (count($paylist) != $limit){
		break ;
	}
	$start = $start + $limit ;
}

sql_query("delete from `user_pay_vip` where unix_timestamp(from_unixtime(end_time,'%Y-%m-%d')) <= unix_timestamp(curdate()) ") ;

log_save_vip("处理结束") ;
	
?>
